package com.liu.webManagent.controller;

import com.liu.common.commons.PageVo;
import com.liu.common.commons.R;
import com.liu.common.enums.HttpStatus;
import com.liu.service.entity.SysRes;
import com.liu.service.service.ISysResService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.poi.ss.formula.functions.T;

import javax.annotation.Resource;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 资源表 前端控制器
 * </p>
 *
 * @author lxh
 * @since 2020-11-29
 */
@Api(tags = {"资源表"})
@RestController
@RequestMapping("/res")
public class SysResController {

    private Logger log = LoggerFactory.getLogger(getClass());
    @Resource
    private ISysResService sysResService;


    @ApiOperation(value = "新增资源表")
    @PostMapping("/add")
    public R add(@RequestBody SysRes sysRes){
        try {
            return R.ok(sysResService.add(sysRes));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.failed(false, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @ApiOperation(value = "删除资源表")
    @GetMapping("/delete")
    public R delete(@RequestParam("id") Long id){
        try {
            return  R.ok(sysResService.delete(id));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.failed(false, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @ApiOperation(value = "更新资源表")
    @PostMapping("/update")
    public R update(@RequestBody SysRes sysRes){
        try {
            return R.ok(sysResService.updateData(sysRes));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.failed(false, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

@ApiOperation(value = "分页查询，参数: ")
    @PostMapping("/pageList")
    public R findListByPage(@RequestBody  @Validated PageVo< T > page){
        // 此处查询参数 自己向里面规范，或者改为vo进行处理 current :当前页，size: 每页条数
        try {
            return R.ok(sysResService.findListByPage(page));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.failed(false, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @ApiOperation(value = "id查询资源表")
    @GetMapping("/getById")
    public R findById(@RequestParam("id") Long id){
        try {
            return R.ok(sysResService.findById(id));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.failed(false, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

}
